import time
import os
from addIntent.demo import Relation, create_lattice_incrementally, read_relation_from_file

def write_results_to_md(file_path: str, num_objects: int, num_attributes: int, num_concepts: int,
                        elapsed_time: float) -> None:
    with open(file_path, 'a') as f:
        f.write("| {0:<11} | {1:<14} | {2:<11} | {3:<18.10f} |\n".format(
            num_objects, num_attributes, num_concepts, elapsed_time))


def main():
    # 文件名数组
    file_paths = [
        '10-100.txt',
        '20-100.txt',
        '30-100.txt',
        '40-100.txt',
        '50-100.txt',
        '60-100.txt',
        '70-100.txt',
        '80-100.txt',
        '90-100.txt',
        '100-100.txt',

    ]

    # 创建结果文件并写入表头
    results_file_path = 'results.md'
    with open(results_file_path, 'w') as f:
        f.write("| num_objects | num_attributes | num_concept | time               |\n")
        f.write("|-------------|----------------|-------------|--------------------|\n")

    for file_path in file_paths:
        print("Processing file: {}".format(file_path))
        start = time.time()

        # 读取关系数据
        num_objects, num_attributes, relation_pairs = read_relation_from_file(file_path)
        rel = Relation(relation_pairs)

        # 自动生成对象数组名和属性数组名
        object_names = [str(i + 1) for i in range(num_objects)]
        attribute_names = [chr(97 + i) for i in range(num_attributes)]

        # 创建概念格
        lattice = create_lattice_incrementally(object_names, attribute_names, rel)

        # 记录结果
        num_concepts = len(lattice.concepts)
        elapsed_time = time.time() - start

        # 输出结果到控制台
        print("Number of concepts: {}".format(num_concepts))
        print("Time taken: {:.2f} seconds".format(elapsed_time))
        print('-' * 40)

        # 写入结果到 Markdown 文件
        write_results_to_md(results_file_path, num_objects, num_attributes, num_concepts, elapsed_time)


if __name__ == '__main__':
    main()
